<?php

namespace yifang\middleware;

class CrossDomain implements Middleware_contract
{
    /**
     *  处理请求
     */
    public function handle($request, \Closure $next)
    {
        // 处理跨域
        header('Access-Control-Max-Age:600');
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
        header('Access-Control-Allow-Credentials:true');
        header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With, X-ApiVersion, X-Lang');

        // 第一次预请求不管
        if (strtoupper($request->method()) == "OPTIONS") {
            exit;
        }

        $response = $next($request);
        return $response;
    }
}
